// https://router.vuejs.org/zh/
import { createRouter, createWebHistory } from 'vue-router'
// import NProgress from 'nprogress'
// import 'nprogress/nprogress.css'
import { Toast } from 'vant'

// 导入路由组件
import mian from '@/views/index.vue'
import login from '@/views/loginview.vue'
import forgot from "@/views/forgot.vue"
// NProgress.configure({ showSpinner: true })

// 定义路由，每个路由都需要映射到一个组件
const routes = [
  {
    path: '/',
    name: 'home',
    component: mian,
    // meta: {
    //   cheakIsLogin: true
    // }
  },
  {
    path: '/login',
    name: 'login',
    component: login,
    // meta: {
    //   cheakIsLogin: true
    // }
  },
  {
    path: '/forgot',
    name: 'forgot',
    component: forgot,
  },

  {
    path: '/menu',
    name: 'menu',
    component: () => import('../views/menu.vue'),
    // meta: {
    //   cheakIsLogin: true
    // }
  },

  {
    path: '/shopbag',
    name: 'shopbag',
    component: () => import('../views/shopbag.vue'),
    meta: {
      cheakIsLogin: true
    }
  },

  {
    path: '/my',
    name: 'my',
    component: () => import('../views/my.vue'),
    meta: {
      cheakIsLogin: true
    }
  },

  {
    path: '/order',
    name: 'Order',
    component: () => import('../views/myview/Order.vue'),
    meta: {
      cheakIsLogin: true
    }
  },

  {
    path: '/search',
    name: 'Search',
    component: () => import('../views/search/search.vue')
  },
  {
    path: '/like',
    name: 'Like',
    component: () => import('../views/myview/Like.vue'),
    meta: {
      cheakIsLogin: true
    }
  },
  {
    path: '/myaddress',
    name: 'MyAddress',
    component: () => import('../views/myview/MyAddress.vue'),
    meta: {
      cheakIsLogin: true
    }
  },
  {
    path: '/account',
    name: 'Account',
    component: () => import('../views/myview/Account.vue'),
    meta: {
      cheakIsLogin: true
    }
  },
  {
    path: '/secure',
    name: 'Secure',
    component: () => import('../views/myview/Secure.vue'),
    meta: {
      cheakIsLogin: true
    }
  },
  {
    path: '/address',
    name: 'Address',
    component: () => import('../views/address/address.vue'),
    meta: {
      cheakIsLogin: true
    }
  },
  {
    path: '/detail/:pid',
    name: 'Detail',
    component: () => import('../views/detail/detail.vue'),
    meta: {
      cheakIsLogin: true
    }
  },

  {
    path: '/orderbuy/:sids',
    name: 'Orderbuy',
    component: () => import('../views/orderbuy/orderbuy.vue'),
    meta: {
      cheakIsLogin: true
    }
  },

]

// 创建路由实例并传递 `routes` 配置
const router = createRouter({
  history: createWebHistory(process.env.VUE_APP_PUBLIC_PATH),
  routes,
  scrollBehavior() {
    // 始终滚动到顶部
    return { top: 0 }
  },
})

router.beforeEach((_to, _from, next) => {
  if (_to.meta.cheakIsLogin) {
    // console.log(_to.meta);
    if (localStorage.getItem("token")) {
      next()
    } else {
      Toast('请登录')
      next('/login')
    }
  } else {
    // router.push('/login')
    next()

  }
  // NProgress.start() // start progress bar
})

// router.afterEach(() => {
//   NProgress.done() // finish progress bar
// })

// 导出路由实例，并在 `main.ts` 挂载
export default router
